//
//  SEGSessionMessageUserDetailViewController.m
//  Commom
//
//  Created by Clavis on 2019/9/29.
//

#import "SEGSessionMessageUserDetailViewController.h"

#import "SEGSessionMessageUserDetailTableViewCell.h"
#import "SEGAlertView.h"

#import "SEGSessionMessageUserDetailViewModel.h"

@interface SEGSessionMessageUserDetailViewController () <UITableViewDelegate, UITableViewDataSource>

/// 表格
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UILabel *nameLabel;

/// viewModel
@property (nonatomic, strong) SEGSessionMessageUserDetailViewModel *viewModel;

@end

@implementation SEGSessionMessageUserDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self requestFriendDetail];
}

#pragma mark - Load Data
- (void)requestFriendDetail {
    @weakify(self);
    [[self.viewModel.command execute:self.userID] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        self.nameLabel.text = self.viewModel.userDict[@"nickname"];
        NSString *headURLString = self.viewModel.userDict[@"ico"] ?: @"";
        [self.headImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,headURLString]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg82")]];
        [self.tableView reloadData];
        
    } error:^(NSError * _Nullable error) {
        [SEGAlertView showTmepMessageWithMessage:error.localizedFailureReason];
    }];
}

#pragma mark - UI
- (void)setupUI {
    self.title = @"详细资料";
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
}

#pragma - <UITableViewDelegate, UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGSessionMessageUserDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailCell" forIndexPath:indexPath];
    cell.tipLabel.text = indexPath.row == 0 ? @"昵称" : @"描述";
    cell.contentLabel.text = indexPath.row == 0 ? self.viewModel.userDict[@"nickname"] : self.viewModel.userDict[@"desc"];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 182;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [UIView new];
    headerView.backgroundColor = kColor_bg;
    [headerView addSubview:self.headImageView];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(50);
        make.centerX.mas_equalTo(0);
        make.width.height.mas_equalTo(84);
    }];
    [self.headImageView seg_configWithRadius:0];
    
    [headerView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.top.equalTo(self.headImageView.mas_bottom).offset(14);
    }];
    
    return headerView;
}

#pragma mark - Getter
- (SEGSessionMessageUserDetailViewModel *)viewModel {
    if (!_viewModel) _viewModel = [[SEGSessionMessageUserDetailViewModel alloc] init];
    return _viewModel;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = kColor_bg;
        _tableView.tableFooterView = [[UIView alloc] init];
        [_tableView registerClass:[SEGSessionMessageUserDetailTableViewCell class] forCellReuseIdentifier:@"detailCell"];
        _tableView.separatorColor = kColor_L2;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 44;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _tableView;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = ({
            UILabel *label = [[UILabel alloc] init];
            label.textColor = kColor_1;
            label.font = kFont_15;
            label;
        });
    }
    return _nameLabel;
}
- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = ({
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"headimg82")];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
            imageView;
        });
    }
    return _headImageView;
}

@end
